﻿using System;
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;

using Gift.DAL;
using Gift.Model;

namespace WebSite.cn.ashx
{
    public class cardAddHandler : IHttpHandler, IRequiresSessionState
    {
        /// <summary>
        /// 您将需要在您网站的 web.config 文件中配置此处理程序，
        /// 并向 IIS 注册此处理程序，然后才能进行使用。有关详细信息，
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        readonly Card theCard = new Card();

        public bool IsReusable
        {
            // 如果无法为其他请求重用托管处理程序，则返回 false。
            // 如果按请求保留某些状态信息，则通常这将为 false。
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            BasePage thePage = new BasePage();

         
            if (HttpContext.Current.Request["code"].ToLower() != context.Session["checkCode"].ToString().ToLower())
            {
                context.Response.Write("0");
                return;
            }

            dbCard oCard;
            try
            {
                oCard = theCard.GetModel(HttpContext.Current.Request["cardCode"].Trim(), HttpContext.Current.Request["cardPwd"].Trim());
            }
            catch
            {
                context.Response.Write("2");
                return;
            }
            if (oCard != null)
            {
                if ((oCard.states == 1 || oCard.states == 5) &&  DateTime.Now.Date <= oCard.endtime && oCard.StartTime <= DateTime.Now.Date)
                {
                    //if (oCard.cardtypeId == 27 || oCard.cardtypeId == 28 || oCard.cardtypeId == 29 || oCard.cardtypeId == 30)
                    //{
                    //    context.Response.Write("104");
                    //    return;
                    //}
                    //if (oCard.cardtypeId == 60 || oCard.cardtypeId == 61 || oCard.cardtypeId == 62 || oCard.cardtypeId == 63
                    //    || oCard.cardtypeId == 64 || oCard.cardtypeId == 65 || oCard.cardtypeId == 66 || oCard.cardtypeId == 67
                    //        || oCard.cardtypeId == 68 || oCard.cardtypeId == 69 || oCard.cardtypeId == 72 || oCard.cardtypeId == 73 || oCard.cardtypeId == 74 
                    //    )
                    //{
                    //    context.Response.Write("104");
                    //    return;
                    //}

                
                    if (thePage.LoginUserMember == null)
                    {
                        thePage.LoginUserMember = new UserMember();
                        thePage.LoginUserMember.MemberId = HttpContext.Current.Request["cardCode"].Trim();
                        thePage.LoginUserMember.IsCard = true;
                    }
                    if (thePage.LoginUserMember.DicUserCardList == null
                        || !thePage.LoginUserMember.DicUserCardList.ContainsKey(oCard.cardid))
                    {
                        if (thePage.LoginUserMember.DicUserCardList == null)
                        {
                            thePage.LoginUserMember.DicUserCardList = new Dictionary<int, dbCard>();
                        }
                        else
                        {
                        
                            foreach (dbCard o in thePage.LoginUserMember.DicUserCardList.Values)
                            {
                                if (o.cardtypeId != oCard.cardtypeId)
                                {
                                    context.Response.Write("4");
                                    return;
                                }
                            }
                        }
                        thePage.LoginUserMember.DicUserCardList[oCard.cardid] = oCard;
                    }

                    context.Response.Write("5");
                }
                else if (oCard.states == 2)
                {
                    context.Response.Write("6");
                }
                else
                {
                    context.Response.Write("3");
                }
            }
            else
            {
                context.Response.Write("2");
            }
        }

        #endregion
    }
}
